package com.sansecy.customview.hook;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.sansecy.customview.R;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.util.List;

public class HookActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hook);
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        final MyAdapter adapter = new MyAdapter();
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        final EditText etInput = findViewById(R.id.et_input);
        final TextView tvContent = findViewById(R.id.tv_content);

        Button btn = findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String string = etInput.getText().toString();
//                try {
//                    Class<?> aClass = Class.forName(string);
////                    Field[] declaredFields = aClass.getDeclaredFields();
//                    Method[] declaredMethods = aClass.getDeclaredMethods();
////                    List<Field> fields = Arrays.asList(declaredFields);
//                    List<Method> methods = Arrays.asList(declaredMethods);
//                    adapter.setMethods(methods);
////                    Method set = aClass.getMethod("set", String.class, String.class);
////                    set.invoke(aClass, "debug.layout", String.valueOf(openDebugLayout));
//                } catch (ClassNotFoundException e) {
//                    e.printStackTrace();
//                    Toast.makeText(getApplicationContext(), "找不到" + e.getMessage(), Toast.LENGTH_LONG).show();
//                }
                tvContent.setText(readSysFile());
            }
        });
        Log.d(this.getClass().getName(), "onCreate:readSysFile() " + readSysFile());
    }

    private String open() {
        String result = "";
        try {
            File file = new File("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
            InputStream in = new FileInputStream(file);
            byte[] re = new byte[32768];
            int read = 0;
            while ((read = in.read(re, 0, 32768)) != -1) {
                String string = new String(re, 0, read);
                Log.e(getClass().getSimpleName(), string);
                result += string;
            }
            in.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return result;
    }

    private String getSysFile() {
        ProcessBuilder cmd;
        String result = "";

        try {
            String[] args = {"/system/bin/cat", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"};
            cmd = new ProcessBuilder(args);

            Process process = cmd.start();
            InputStream in = process.getInputStream();
            byte[] re = new byte[32768];
            int read = 0;
            while ((read = in.read(re, 0, 32768)) != -1) {
                String string = new String(re, 0, read);
                Log.e(getClass().getSimpleName(), string);
                result += string;
            }
            in.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return result;
    }

    private StringBuilder readSysFile() {
        BufferedReader buffered_reader = null;
        StringBuilder builder = new StringBuilder(4605);

        try {
            InputStream istream = Runtime.getRuntime().exec("cat /sys/class/neostra_gpioctl/dev/gpioctl").getInputStream();
            Log.d(this.getClass().getName(), "readSysFile:1 ");
            InputStreamReader istream_reader = new InputStreamReader(istream);
            Log.d(this.getClass().getName(), "readSysFile:2 ");
            buffered_reader = new BufferedReader(istream_reader);
            Log.d(this.getClass().getName(), "readSysFile:3 ");
//            buffered_reader = new BufferedReader(new FileReader("/sys/class/neostra_gpioctl/dev/gpioctl"));
            String line;

            while ((line = buffered_reader.readLine()) != null) {
                builder.append(line).append("\n");
                Log.d(this.getClass().getName(), "readSysFile: " + line);
            }
        } catch (IOException e) {
            Log.d(this.getClass().getName(), "readSysFile: IOException " + e.getMessage());
            Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            e.printStackTrace();
        } finally {
            try {
                if (buffered_reader != null)
                    buffered_reader.close();
            } catch (IOException ex) {
                Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_LONG).show();
                ex.printStackTrace();
            }
        }
        return builder;
    }

    class MyAdapter extends RecyclerView.Adapter<MyHolder> {
        private List<Method> mMethods;

        public void setMethods(List<Method> methods) {
            mMethods = methods;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(android.R.layout.simple_list_item_1, viewGroup, false);
            return new MyHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
            TextView itemView = (TextView) myHolder.itemView;
            itemView.setText(mMethods.get(i).getName());
        }

        @Override
        public int getItemCount() {
            return mMethods == null ? 0 : mMethods.size();
        }
    }

    class MyHolder extends RecyclerView.ViewHolder {

        public MyHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
